<?
class EditProviderForm extends Form
{
	private $provider=array();
	
	function EditProviderForm()
	{
		Form::Form('EditProviderForm');
		if(Url::get('cmd')=='edit'){
            CGlobal::$website_title = "Sửa Nhà cung cấp";
			$id = (int)Url::get('id');
			if($id){
				$this->provider=DB::select('provider','id='.$id);
			}
			if(!$this->provider||$this->provider['status']==-1){
                Url::redirect_current();
            }
		}else{
            CGlobal::$website_title = "Thêm Nhà cung cấp";
			$this->provider=array('name'=>'','avatar'=>'','img_url'=>'','desc'=>'','email'=>'', 'website'=>'','company'=>'','address'=>'','tel'=>'','mobile'=>'','fax'=>'');
		}
        $this->link_css("style/provider.css");
	}
	
	function on_submit(){
		$provider["name"]		= Url::getParam('name','');
        $provider["desc"]		= Url::getParam('desc','');
        $provider["email"]		= Url::get('email','');
        $provider["website"]	= Url::getParam('website','');
        $provider["company"]	= Url::getParam('company','');
        $provider["address"]	= Url::getParam('address','');
        $provider["tel"]		= Url::getParam('tel','');
        $provider["mobile"]		= Url::getParam('mobile','');
        $provider["fax"]		= Url::getParam('fax','');
        
        $valid = true;
        if($provider["name"]==''){
            $valid = false;
            $this->setFormError("error_name","Tên nhà cung cấp không được để trống!");
        }
        if($provider["email"]){
			if(!preg_match("/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6}$/i",$provider["email"]) ){
				$valid = false;
                $this->setFormError("error_email","Email không hợp lệ!");
			}
		}
        
		if($valid){
            $avatar = $_FILES["avatar"]["name"];
            if($avatar){
                if($_FILES["avatar"]["tmp_name"]){
                    if(Upload::storage_check_dir("provider/", true, IMAGE_SERVER_NO))
                    {
                        $img_ext = explode(".", $avatar);
                        $file_ext = array_pop($img_ext);
                        if(!in_array($file_ext,array('jpg','png','jpeg','gif'))){
                            $this->setFormError("error_file_ext","File tải lên không đúng định dạng!");
                            return ;
                        }
                        $file_name = UP_FOLDER."/provider/" . String::_name_cleaner(Unicode::stripUnicode($provider["name"]),'-').'.'.$file_ext ;
                        $sourceName = $_FILES["avatar"]["tmp_name"];
                        if(!Upload::storage_put_file($file_name, $sourceName, IMAGE_SERVER_NO, FTP_BINARY,1)){
                            $this->setFormError("error_upload_file","Có lỗi xảy ra khi upload file!");
                            return ;
                        };
                        $provider["img_url"] = $file_name;
                        $provider['img_server'] = IMAGE_SERVER_NO;
                    }
                }
            }

			$id = Url::get("id");

            if($id==''){//create
                $provider["status"] = 1;
                $provider["time_c"] = time();
                $provider["user_id_c"] = User::id();
                $provider["user_email_c"] = User::$current->data['email'];
                $check = Db_Provider::insert($provider);
            }else{//edit
                $provider["time_m"] = time();
                $provider["user_id_m"] = User::id();
                $provider["user_email_m"] = User::$current->data['email'];
                $check = Db_Provider::update($provider,$id);
            }
            if($check){
                //Thanh cong
                $this->setFormSucces("success","Save hành công!");
                Url::redirect_current();
            }else{
                $this->setFormError("error_db_action","Có lỗi xảy ra");
            }
		}
	}	
	
	function draw()
	{	
		global $display;
		$this->beginForm(true);
        $this->provider['name'] = Url::get('name',$this->provider['name']);
        $this->provider['avatar'] = Db_Provider::getImageUrl($this->provider['img_url']);
        $this->provider['desc'] = Url::get('desc',$this->provider['desc']);
        $this->provider['email'] = Url::get('email',$this->provider['email']);
        $this->provider['website'] = Url::get('website',$this->provider['website']);
        $this->provider['company'] = Url::get('company',$this->provider['company']);
        $this->provider['address'] = Url::get('company',$this->provider['address']);
        $this->provider['tel'] = Url::get('tel',$this->provider['tel']);
        $this->provider['mobile'] = Url::get('mobile',$this->provider['mobile']);
        $this->provider['fax'] = Url::get('fax',$this->provider['fax']);
        $display->add('provider',$this->provider);

        if(Url::get('cmd')=='edit')
        {
            $display->add('mode',"SỬA NHÀ CUNG CẤP");
            $display->add('title_submit',"Sửa");
            $display->add('cmd',"edit");
            /*$user_c = User::getUser($this->provider['user_id_c']);
            $display->add('user_name_c',$user_c['email']);
            $user_m = User::getUser($this->provider['user_id_m']);
            if($user_m){
                $display->add('user_name_m',$user_m['email']);
            }*/
        }
        else
        {
            $display->add('mode',"THÊM NHÀ CUNG CẤP");
            $display->add('title_submit',"Thêm");
            $display->add('cmd',"add");
            if($this->showFormSuccesMessages(1)!='')//submit thành công
            {
                $display->add('provider',$this->provider);
            }
        }
        $display->add('message',$this->showFormErrorMessages(1));
        $display->add("success",$this->showFormSuccesMessages(1));
        $display->output('ProviderEdit');
		$this->endForm();
	}
}
?>